package sort;

/**
 * 快速排序
 * 时间复杂度：O(logn)
 * 空间复杂度：O(n)
 */
public class QuickSort {

    public static void quickSort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }

        int pivot = arr[left];
        int i = left;
        int j = right;

        while (i < j) {
            while (i < j && arr[j] >= pivot) {
                j--;
            }

            arr[i] = arr[j];
            while (i < j && arr[i] <= pivot) {
                i++;
            }
            arr[j] = arr[i];
        }

        arr[j] = pivot;

        quickSort(arr, left, i - 1);
        quickSort(arr, i + 1, right);

    }

    public static void main(String[] args) {
        int[] nums = { 10, 20, -19, 30, 90, 43, 67, 5 };
        quickSort(nums, 0, nums.length - 1);
        for (int i : nums) {
            System.out.println(i);
        }
    }
}
